package org.zxstudio.encrypt;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.zxstudio.encrypt.utils.LazyImageLoader;
import org.zxstudio.encrypt.utils.SDUtils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FileParentAdapter extends BaseAdapter {

	private static final String TAG = "FileChildAdapter";
	private static final boolean DEBUG = true;

	public static final int VIEWMODE_LIST = 0;
	public static final int VIEWMODE_ICON = 1;

	private Context mContext;
	private List<File> mFiles = null;

	private int mViewMode = VIEWMODE_ICON;

	private LazyImageLoader mLoader;

	public FileParentAdapter(Context context, List<File> files) {
		mContext = context;
		mFiles = files;
		mLoader = LazyImageLoader.getInstance();
	}

	@Override
	public int getCount() {
		if (mFiles != null) {
			return mFiles.size();
		}
		return 0;
	}

	@Override
	public File getItem(int position) {

		if ((position >= 0) && (position < this.getCount())) {
			return mFiles.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ListHolder listHolder = null;
		GridHolder gridHolder = null;
		switch (mViewMode) {
		case VIEWMODE_LIST: {
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.parent_item_listview, null);
				listHolder = new ListHolder();
				listHolder.parentView = convertView.findViewById(R.id.parent_view);
				listHolder.fileIcon = (ImageView) convertView
						.findViewById(R.id.listview_fileicon);
				listHolder.fileName = (TextView) convertView
						.findViewById(R.id.listview_filename);
		/*		listHolder.fileSize = (TextView) convertView
						.findViewById(R.id.listview_filesize);
				listHolder.fileTime = (TextView) convertView
						.findViewById(R.id.listview_filetime);*/
				convertView.setTag(listHolder);
			} else {
				listHolder = (ListHolder) convertView.getTag();
			}
			
			File f = getItem(position);
			if (f != null) {
				currentParent(position,listHolder);
				int icon = this.getFileIcon(f);
				if (icon == -1) {
					Drawable drawable = this.getApkIcon(f.getAbsolutePath());
					if (drawable != null) {
						listHolder.fileIcon.setImageDrawable(drawable);
					} else {
						listHolder.fileIcon
								.setImageResource(R.drawable.icon_file);
					}
				} else {
					listHolder.fileIcon.setImageResource(icon);
				}
				if (SDUtils.isImageFile(f)) {
					mLoader.loadImage(listHolder.fileIcon, f.getAbsolutePath(),
							mLoaderListener);
				}
				if (SDUtils.isVideoFile(f)) {
					mLoader.loadVideo(listHolder.fileIcon, f.getAbsolutePath(),
							mLoaderListener);
				}
				listHolder.fileName.setText(f.getName());
			}
		}
			break;
		case VIEWMODE_ICON: {
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_gridview, null);
				gridHolder = new GridHolder();
				gridHolder.fileIcon = (ImageView) convertView
						.findViewById(R.id.gridview_fileicon);
				gridHolder.fileName = (TextView) convertView
						.findViewById(R.id.gridview_filename);
				convertView.setTag(gridHolder);
			} else {
				gridHolder = (GridHolder) convertView.getTag();
			}

			File f = this.getItem(position);
			if (f != null) {
				int icon = this.getFileIcon(f);
				if (icon == -1) {
					Drawable drawable = this.getApkIcon(f.getAbsolutePath());
					if (drawable != null) {
						gridHolder.fileIcon.setImageDrawable(drawable);
					} else {
						gridHolder.fileIcon
								.setImageResource(R.drawable.icon_file);
					}
				} else {
					gridHolder.fileIcon.setImageResource(icon);
				}
				if (SDUtils.isImageFile(f)) {
					mLoader.loadImage(gridHolder.fileIcon, f.getAbsolutePath(),
							mLoaderListener);
				}
				if (SDUtils.isVideoFile(f)) {
					mLoader.loadVideo(gridHolder.fileIcon, f.getAbsolutePath(),
							mLoaderListener);
				}
				gridHolder.fileName.setText(f.getName());
			}
		}
			break;
		default:
			break;
		}

		return convertView;
	}

	public int getViewMode() {
		return mViewMode;
	}

	private void currentParent(int position,ListHolder holder) {
	
		if (mFiles.size() >= 1) {
			if (position == mFiles.size()-1) {
				if (DEBUG) {
					Log.v(TAG,"highlight current parent ");
				}
				holder.parentView.setBackgroundResource(R.drawable.parent_hightlight);
				//holder.parentView.requestFocus();
			} else {
				if (DEBUG) {
					Log.v(TAG,"refresh white color");
				}
				//holder.parentView.setBackgroundDrawable(null);
				holder.parentView.setBackgroundResource(R.drawable.parent_df);
			}
		}
	
		
	}
	public void setViewMode(int viewmode) {
		mViewMode = viewmode;
	}

	public int getFileIcon(File f) {
		int icon = 0;

		String str = org.zxstudio.encrypt.utils.Constant.getFileIcon(f);
		if (str == null) {
			icon = -1;
		} else {
			Resources res = mContext.getResources();
			icon = res
					.getIdentifier(str, "drawable", mContext.getPackageName());

			if (icon <= 0)
				icon = R.drawable.icon_file;
		}

		return icon;
	}

	public Drawable getApkIcon(String path) {
		PackageManager pm = mContext.getPackageManager();
		PackageInfo info = pm.getPackageArchiveInfo(path,
				PackageManager.GET_ACTIVITIES);
		if (info != null) {
			ApplicationInfo appInfo = info.applicationInfo;

			if (Build.VERSION.SDK_INT >= 8) {
				appInfo.sourceDir = path;
				appInfo.publicSourceDir = path;
			}

			return appInfo.loadIcon(pm);
		}
		return null;
	}

	public String getFileTime(long filetime) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String ftime = formatter.format(new Date(filetime));
		return ftime;
	}

	public String getFileSize(long filesize) {
		DecimalFormat df = new DecimalFormat("#.00");
		StringBuffer mstrbuf = new StringBuffer();

		if (filesize < 1024) {
			mstrbuf.append(filesize);
			mstrbuf.append(" B");
		} else if (filesize < 1048576) {
			mstrbuf.append(df.format((double) filesize / 1024));
			mstrbuf.append(" K");
		} else if (filesize < 1073741824) {
			mstrbuf.append(df.format((double) filesize / 1048576));
			mstrbuf.append(" M");
		} else {
			mstrbuf.append(df.format((double) filesize / 1073741824));
			mstrbuf.append(" G");
		}

		df = null;

		return mstrbuf.toString();
	}

	static class ListHolder {
		View parentView;
		ImageView fileIcon;
		TextView fileName;
		TextView fileSize;
		TextView fileTime;
	}

	static class GridHolder {
		ImageView fileIcon;
		TextView fileName;
	}

	private LazyImageLoader.OnImageLoaderListener mLoaderListener = new LazyImageLoader.OnImageLoaderListener() {

		@Override
		public void onImageLoad(ImageView t, Drawable drawable) {
			if (DEBUG) {
				Log.v(TAG, "onImageLoad");
			}
			t.setImageDrawable(drawable);
		}

		@Override
		public void onError(ImageView t) {
			if (DEBUG) {
				Log.v(TAG, "onError....................");
			}
		}

		@Override
		public void onImageLoad(ImageView t, Drawable drawable, int type) {
			// TODO Auto-generated method stub
			
		}

	};

}
